package com.easonji.admin.controller;



import com.easonji.common.base.controller.BaseController;

import com.easonji.core.annotion.Permission;
import com.easonji.core.log.LogObjectHolder;
import com.easonji.common.base.query.BaseQuery;
import com.easonji.common.base.response.Response;
import com.easonji.common.base.response.enums.ResponseEnum;
import com.easonji.common.util.StringUtils;

import com.easonji.admin.service.IUserUserPostService;
import com.easonji.admin.model.UserUserPost;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.BindingResult;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * <p>
 * 员工上下级关系及职务管理 前端控制器
 * </p>
 *
 * @author easonji
 * @since 2018-10-01
 */
@Controller
@RequestMapping("/admin/userUserPost")
public class UserUserPostController extends BaseController {


    /**
    * 员工上下级关系及职务管理页面前缀
    */
    private final static String PREFIX = "admin/userUserPost/";

    @Resource
    private IUserUserPostService userUserPostService;

    /**
    * 跳转到员工上下级关系及职务管理首页
    */
    @Permission
    @GetMapping("")
    public String index() {
        return PREFIX + "userUserPost";
    }

    /**
    * 查询员工上下级关系及职务管理列表
    */
    @ResponseBody
    @PostMapping("/list")
    public Object queryList(BaseQuery baseQuery) {
        Object result = userUserPostService.queryList(baseQuery);
        return result;
    }

    /**
    * 跳转到添加员工上下级关系及职务管理页面
    */
    @Permission
    @GetMapping("/to_add")
    public String toAdd(Model model) {
        return PREFIX + "userUserPost_edit";
    }

    /**
    * 跳转到修改员工上下级关系及职务管理页面
    */
    @Permission
    @GetMapping("/to_update/{sId}")
    public String toUpdate(@PathVariable Integer sId, Model model) {
        Object result = userUserPostService.queryRowById(sId);
        LogObjectHolder.me().set(result);
        model.addAttribute("userUserPost", result);
        return PREFIX + "userUserPost_edit";
    }

    /**
    * 新增员工上下级关系及职务管理
    */
    @ResponseBody
    @Permission
    @PostMapping("/save")
    public Response add(@Valid UserUserPost userUserPost, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(bindingResult);
        }
        int result = userUserPostService.save(userUserPost);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.SAVE_SUCCESS);
    }

    /**
    * 修改员工上下级关系及职务管理
    */
    @ResponseBody
    @Permission
    @PostMapping("/update")
    public Response update(@Valid UserUserPost userUserPost, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(bindingResult);
        }
        int result = userUserPostService.update(userUserPost);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
    * 删除员工上下级关系及职务管理
    */
    @ResponseBody
    @Permission
    @PostMapping("/remove")
    public Response remove(@RequestParam Integer sId) {
        if (StringUtils.isEmpty(sId)) {
            return failure(ResponseEnum.PARAMETER_ERROR);
        }
        int result = userUserPostService.delete(sId);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.REMOVE_SUCCESS);
    }

}
